/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.controller;

import com.bayu.services.dao.AvgMouldingDao;
import com.bayu.services.entity.AvgMoulding;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Fikri
 */

@Controller
@RequestMapping("/avg-moulding-input.htm")
public class AvgMouldingInput {

    AvgMouldingDao avgMouldingDao;
    @Autowired
    private void setAvgMouldingDao(AvgMouldingDao avgMouldingDao){
      this.avgMouldingDao = avgMouldingDao;
    }

    @RequestMapping(method = RequestMethod.GET)
    public void displayForm(ModelMap map,String id,String typemould){
       map.addAttribute("typemould", typemould);
    }

    @RequestMapping(method = RequestMethod.POST)
    public String proccessForm(@ModelAttribute("AvgMoulding") AvgMoulding avgMoulding , BindingResult result,ModelMap map, String date,HttpServletRequest req){

            DateFormat formarter = new SimpleDateFormat("yy-MM-dd");
            Date tgl;
            String type = req.getParameter("typemould");
            String strReturn ="";

            //exchangeCurrency.setDate(tgl);
            if(result.hasFieldErrors("date")){
                try {
                    tgl = formarter.parse(date);
                    avgMoulding.setDate(tgl);

                } catch (ParseException ex) {
                    Logger.getLogger(CompInput.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

            List total = this.avgMouldingDao.custom("select count(*) from AvgMoulding where date='"+date+"' and type='"+type+"'");

            

            String strTotal=total.get(0).toString();


            int c = Integer.valueOf(strTotal).intValue();
            System.out.println(strTotal+"---"+c+"--"+date);
            if(c > 0){
                return "redirect:ErrorDuplicate.html";
            }else{

                 this.avgMouldingDao.save(avgMoulding);
                 if(type.equals("linemoulding")){
                    strReturn= "redirect:view-line-moulding.htm?msg=Data has been updated";
                 }
                 if(type.equals("tapping")){
                    strReturn= "redirect:view-tapping-data.htm?msg=Data has been updated";
                 }
                 if(type.equals("cnc")){
                    strReturn= "redirect:view-cnc-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mccomp")){
                    strReturn= "redirect:view-machining-comp-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mcp1")){
                    strReturn= "redirect:view-machining-p1-data.htm?msg=Data has been updated";
                 }
                 if(type.equals("operation-mc")){
                    strReturn= "redirect:view-operation-machining-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldfoundry")){
                    strReturn= "redirect:view-mould-foundry-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldpms")){
                    strReturn= "redirect:view-tps-mould-pms-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mouldsm60")){
                    strReturn= "redirect:view-tps-mould-sm60-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("moulddisa")){
                    strReturn= "redirect:view-tps-mould-disa-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("mould2ml")){
                    strReturn= "redirect:view-tps-mould-2ml-data.htm?msg=Data has been updated";
                 }
                 
                 if(type.equals("shaft2w")){
                    strReturn= "redirect:view-tps-mould-shaft2w-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("shaft4w")){
                    strReturn= "redirect:view-tps-mould-shaft4w-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("machining1")){
                    strReturn= "redirect:view-tps-machining1-data.htm?msg=Data has been updated";
                 }

                 if(type.equals("machining2")){
                    strReturn= "redirect:view-tps-machining2-data.htm?msg=Data has been updated";
                 }


                 return strReturn;
            }


    }

}
